package cn.itsource.org.service.Impl;

import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.Departmentmapper;
import cn.itsource.org.query.BaseQuey;
import cn.itsource.org.service.Departservice;

import cn.itsource.org.util.JSONResult;
import cn.itsource.org.util.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.reflect.generics.tree.Tree;


import java.util.List;


@Service
public class DepartserviceImpl implements Departservice {
    @Autowired
    private Departmentmapper departmentmapper;



    @Override
    public JSONResult findPage(BaseQuey baseQuey) {
        //1.调用分页对象
        PageHelper.startPage(baseQuey.getPage(),baseQuey.getRows());
        //2.查询所有
        List<Department> all = departmentmapper.findPage(baseQuey);
        //3.封装分页对象
        PageInfo<Department> departmentPageInfo = new PageInfo<>(all);
        return JSONResult.success(departmentPageInfo);
    }


    @Override
    public JSONResult findtree() {
        //1.查询所有
        List<Department> department= departmentmapper.selectAll();
        List<Department> tree = TreeUtil.getTree(department);
        return JSONResult.success(tree);

    }

    @Override
    public JSONResult add(Department department) {
        departmentmapper.insertSelective(department);
        return JSONResult.success();
    }

    @Override
    public JSONResult update(Department department) {

            departmentmapper.updateByPrimaryKeySelective(department);
            return JSONResult.success();
    }

    @Override
    public JSONResult delete(Long id) {
        Department department = new Department(id, Boolean.FALSE);
        departmentmapper.updateByPrimaryKeySelective(department);
        return JSONResult.success();
    }

    @Override
    public JSONResult deletebatch(List<Long> ids) {
        departmentmapper.deleteBatch(ids);
        return JSONResult.success();
    }


}
